/*
 * 
 * 
 */
package com.pclong.fightlandlordserver;

import com.pclong.fightlandlordserver.util.PathUtil;
import com.pclong.fightlandlordserver.util.PropertiesHelper;
import java.io.File;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

/**
 *@date 2012-3-11,1:56:01
 * @author long
 * @email 405409357@qq.com
 * @description ConfigService类
 */
public class ConfigService {

    private static final Logger logger = LogManager.getLogger(ConfigService.class);
    private static FileObserver ob;
    private static FileListener listener = new FileListener();
    private static FileMonitor monitor;
    private static Serializer serializer = new Persister();
    private static boolean isRun = false;

    static {

        StartConfig(F3ServerProxy.isWeb);
    }

    public static void StartConfig(boolean isWeb) {
        try {
            if (!isRun) {
                setTankAgentConfigFile(PropertiesHelper.getProperty("tankweb.properties", "tankAgentConfigFile"), isWeb);
                StartListener();
                isRun = true;
            }
        } catch (Exception ex) {
        }
    }

    public ConfigService() {
    }

    /**
     * Set the value of tankAgentConfigFile
     *
     * @param tankAgentConfigFile new value of tankAgentConfigFile
     */
    public static synchronized void setTankAgentConfigFile(String tankAgentConfigFile, boolean isWeb) throws Exception {
        // 相对绝对路径转换
        PathUtil p = new PathUtil();
        logger.info("配置路径：" + tankAgentConfigFile);
        tankAgentConfigFile = p.getAasolutePath(tankAgentConfigFile, isWeb);
        logger.info("转换后的路径：" + tankAgentConfigFile);
        logger.info("init tankAgentConfigFile:" + tankAgentConfigFile);
        ConfigFileManager.getInstance().ListenerConfigDir = tankAgentConfigFile.substring(0, tankAgentConfigFile.lastIndexOf("\\"));

        ConfigFileManager.getInstance().TankAgentConfigFile = tankAgentConfigFile;
        File file = new File(ConfigFileManager.getInstance().TankAgentConfigFile);
        if (!file.exists()) {
            SaveTankAgentConfig();
            logger.info("init1 Create file :" + tankAgentConfigFile);
        } else {
            RestoreTankAgentConfig();
            logger.info("init1 Load file :" + tankAgentConfigFile);
        }


    }

    public static void StartListener() {

        logger.info("StartListenere:" + ConfigFileManager.getInstance().ListenerConfigDir);
        ob = new FileObserver(ConfigFileManager.getInstance().ListenerConfigDir);
        ob.addListener(listener);
        monitor = new FileMonitor(ob);
        monitor.start();
    }

    public static void StopListener() {
        if (monitor != null) {
            monitor.stop();
        }
    }

    /**
     * Get the value of TankAgentConfig
     *
     * @return the value of TankAgentConfig
     */
    public static TankAgentConfigEntity getTankAgentConfig() {

        return TankAgentConfigEntity.getInstance();
    }

    public synchronized static void SaveTankAgentConfig() throws Exception {
        File file = new File(ConfigFileManager.getInstance().TankAgentConfigFile);
        serializer.write(TankAgentConfigEntity.getInstance(), file);

    }

    public static void RestoreTankAgentConfig() throws Exception {
        File file = new File(ConfigFileManager.getInstance().TankAgentConfigFile);
        TankAgentConfigEntity.setInstance(serializer.read(TankAgentConfigEntity.class, file));

    }
}
